package com.example.jl.bethinner.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jl.bethinner.R;
import com.example.jl.bethinner.databinding.ActivityRxjavaBinding;
import com.example.jl.bethinner.domain.User;
import com.ogaclejapan.rx.binding.Rx;
import com.ogaclejapan.rx.binding.RxUtils;
import com.ogaclejapan.rx.binding.RxView;

import java.util.List;

import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

public class RxjavaActivity extends AppCompatActivity {

    private TextView textView;
    private Button button;
    private User user;
    private ActivityRxjavaBinding binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         binding = DataBindingUtil.setContentView(
                this, R.layout.activity_rxjava);
         user = new User("fei", "Liang",10);
        binding.setUser(user);
        textView = (TextView)findViewById(R.id.text_view);


        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user=new User("王","强",23);
                binding.setUser(user);
            }
        });
        final Observable<String> myObservable = Observable.create(
                new Observable.OnSubscribe<String>() {
                    @Override
                    public void call(Subscriber<? super String> sub) {
                        sub.onNext("Hello, world!");
                        sub.onCompleted();
                    }
                }
        );

        Subscriber<String> mySubscriber = new Subscriber<String>() {
            @Override
            public void onNext(String s) {
                showToast(s);
            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }
        };

//        myObservable.doOnNext()


        //学习map的使用
//        InputString inputString = new InputString(100l, "hello,RxJava");
//        Observable.just(inputString).map(new Func1<InputString, String>() {
//            @Override
//            public String call(InputString inputString) {
//                return inputString.getInString();
//            }
//        }).subscribe(new Action1<String>() {
//            @Override
//            public void call(String s) {
//                showToast(s);
//            }
//        });


        Subscriber<Integer> integerSubscriber = new Subscriber<Integer>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer integer) {
                textView.setText("合计" + integer);
            }
        };

        Observable.just("1","2","2","3","4","5")
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, Integer>() {
                    @Override
                    public Integer call(String s) {
                        return Integer.valueOf(s);
                    }
                })//map操作符将String转化为integer
                .filter(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        return integer>1;
                    }
                })//filter操作符过滤<=1的数字
                .distinct()//去重
                .take(3)//前三个
                .reduce(new Func2<Integer, Integer, Integer>() {
                    @Override
                    public Integer call(Integer integer, Integer integer2) {
                        return integer.intValue()+integer2.intValue();
                    }
                })//reduce操作符 求和
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(integerSubscriber);






    }
    private Observable<Long> DeferObserver() {
        return Observable.defer(new Func0<Observable<Long>>() {
            @Override
            public Observable<Long> call() {
                return Observable.just(100l);
            }
        });
    }


    private void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();

    }
}
